package com.ruoyi.wisdom.task;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.wisdom.domain.swap.SwapCompanyUser;
import com.ruoyi.wisdom.event.CompanyUserEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class CompanyUserTask extends IotTask {

    @Autowired
    private CompanyUserEvent companyUserEvent;

    @Scheduled(fixedRate = 10000) // 每10秒执行一次
    public void pullTask() {
        printLog("拉取职工数据");
        try {
            Long length = redisSecondTemplate.opsForList().size(iotConstants.getPullCompanyUer());
            if (length == null) return;
            while (length > 0) {
                Object obj = redisSecondTemplate.opsForList().leftPop(iotConstants.getPullCompanyUer());
                if (obj != null) {
                    String objJson = obj.toString();
                    SwapCompanyUser swapCompanyUser = JSON.parseObject(objJson, SwapCompanyUser.class);
                    companyUserEvent.handle(swapCompanyUser);
                }
                length--;
            }
        } catch (Exception e) {
            printErrorLog("拉取教职工数据-- redis 队列-异常：" + e.getMessage());
        }
    }
}
